// index.js
import {Goods} from "../../model/goods";

Page({
    data: {
        bannerList:null,
        spuNewList: null,
        spuHotList: null
    },

    async onLoad(options) {
        const goods = new Goods();

        const [bannerList, spuHotList, spuNewList] = await Promise.all([
            goods.getBannerList(),
            goods.getHotGoods(),
            goods.getNewGoods()
        ]);

        const processedBannerList = bannerList.map(this.processBannerImage);
        const processedHotList = spuHotList.map(this.processHotImage)
        this.setData({
            spuNewList,
            spuHotList: processedHotList,
            bannerList: processedBannerList
        })

        wx.lin.renderWaterFlow(spuNewList)      
    },

    processBannerImage(item) {
        if(item.img) {
            const processedImageUrl =  `${item.img}?imageView2/0/w/375/h/200`;
            return {
                ...item,
                img: processedImageUrl
            }
        }
    },
    processHotImage(item) {
        if(item.picUrl) {
            const processedImageUrl =  `${item.picUrl}?imageView2/0/w/120/h/120`;
            return {
                ...item,
                picUrl: processedImageUrl
            }
        }
    },
    
    onGotoSearch(event) {
        wx.navigateTo({
            url: `/pages/search/search`
        })
    },
})
